﻿# Extensions

$.rhd = $.rhd || {}
$.rhd.grid = $.rhd.grid || {}

$.extend $.rhd.grid,
    version: "1.0"

# Formatters

$.rhd.grid.formatter = $.rhd.grid.formatter || {}

$.extend $.rhd.grid.formatter,
    string: (val, opt) ->
        return "" if not val?
        val.toString()

    integer: (val, opt) ->
        return "" if not val?
        val.toString()

    numeric: (val, opt) ->
        return "" if not val?
        numeric = new NumberFormat()
        numeric.setInputDecimal '.'
        numeric.setNumber val
        numeric.setPlaces opt.decimalPlaces, true
        numeric.setCurrency false
        numeric.setNegativeFormat numeric.LEFT_DASH
        numeric.setSeparators true, opt.thousandsSeparator, opt.decimalSeparator
        numeric.toFormatted()

    datetime: (val, opt) ->
        return "" if not val?
        val = new Date(val) if $.isNumeric(val) or typeof val is "string"
        val.format opt.mask

    boolean: (val, opt) ->
        if val then $.rhd.grid.locale.yes else $.rhd.grid.locale.no

# Renderes

$.rhd.grid.renderer = $.rhd.grid.renderer || {}

$.extend $.rhd.grid.renderer,
    _default: (td, value, col, row, opt) ->
        td.css "text-align", col.textAlign if col.textAlign?
        span = $ "<span>"
        span.appendTo td

        if value and opt.maxLength > 0 and value.length > opt.maxLength
            span.prop "title", value
            span.text value.substr(0, opt.maxLength) + "..."
        else
            span.text value

    multiselect: (td, value, col, row, opt) ->
        check = $ "<input>", { type: "checkbox", class: "multiselect" }
        check.appendTo td

        if row[col.dataField]
            check.get(0).checked = true
            td.parent('tr').addClass "warning"

        check.change =>
            if check.is ":checked"
                td.parent('tr').addClass "warning"
                row[col.dataField] = true
            else
                td.parent('tr').removeClass "warning"
                row[col.dataField] = false
            @updatedMultiSelect()

$.rhd.grid.header = $.rhd.grid.header || {}

$.extend $.rhd.grid.header,
    _default: (th, col, opt) ->
        th.css "width", col.width if col.width?
        th.css "text-align", col.textAlign if col.textAlign?

        title = $ "<span>"
        title.append col.title || "&nbsp;"
        title.append @makeIconSort col
        title.appendTo th
        title.prop "title", col.tooltip if col.tooltip?

        if col.sort? and col.sort.sortable
            title.css "cursor", "pointer"
            title.click (event) =>
                @toggleSort col, event.ctrlKey

    multiselect: (th, col, opt) ->
        th.prop "class", "multiselect"
        group = $ "<div>", { class: "btn-group" }
        group.appendTo th

        @check = $ "<input>", { type: "checkbox", class: "multiselect" }
        @check.appendTo group
        @check.get(0).checked = @pageChecked()
        @check.change =>
            @checkUncheckPage @check.is ":checked"

        btnOptions = $ "<a>", { class: "dropdown-toggle", "data-toggle": "dropdown", href: "#" }
        btnOptions.append $ "<span>", { class: "caret" }
        btnOptions.appendTo group

        btnOptionsMenu = $ "<ul>", { class: "dropdown-menu" }
        btnOptionsMenu.appendTo group

        btnOptionsMenu.append $("<li>").append $("<a>").append($.rhd.grid.locale.multiselect.checkAll).click =>
            @checkAll()
        btnOptionsMenu.append $("<li>").append $("<a>").append($.rhd.grid.locale.multiselect.uncheckAll).click =>
            @uncheckAll()
        btnOptionsMenu.append $("<li>").append $("<a>").append($.rhd.grid.locale.multiselect.invert).click =>
            @invert()

# Sorters

$.rhd.grid.sorter = $.rhd.grid.sorter || {}

$.extend $.rhd.grid.sorter,
    string: (a, b) ->
        a = if a then new String(a).toLowerCase() else ""
        b = if b then new String(b).toLowerCase() else ""

        return 0 if a is b
        return -1 if a < b
        1

    integer: (a, b) ->
        a = parseFloat a
        a = 0 if isNaN a

        b = parseFloat b
        b = 0 if isNaN b

        a - b

    numeric: (a, b) ->
        a = parseFloat a
        a = 0 if isNaN a

        b = parseFloat b
        b = 0 if isNaN b

        a - b

    datetime: (a, b) ->
        a = if a then a else new Date()
        b = if b then b else new Date()

        return 0 if a is b
        return -1 if a < b
        1

    boolean: (a, b) ->
        a = if a then a else new Boolean()
        a = a.valueOf()

        b = if b then b else new Boolean()
        b = b.valueOf()

        return 0 if a is b
        return -1 if a < b
        1

# Functions

$.rhd.grid.functions = $.rhd.grid.functions || {}

$.extend $.rhd.grid.functions,
    getValue: (dataField, value) ->
        return null if not dataField or not value
        value[dataField]

